package dataserver.service.impl;

import dataserver.mapper.SceneDeviceRelationRepository;
import dataserver.mapper.SceneDeviceRepository;
import dataserver.pojo.SceneDevice;
import dataserver.pojo.SceneDeviceRelation;
import dataserver.service.SceneDeviceRelationService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;


@Service
public class SceneDeviceRelationServiceImpl implements SceneDeviceRelationService {
    private static Logger logger = LogManager.getLogger(SceneDeviceRelationServiceImpl.class.getName());

    @Autowired
    private SceneDeviceRelationRepository sceneDeviceRelationRepository;


    @Autowired
    private SceneDeviceRepository sceneDeviceRepository;

    /**
     * 存储信息
     *
     * @param sourceData
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(SceneDeviceRelation sourceData) throws Exception {
        int t = 0;
        try {
            if (sourceData.getId() == 0) {
                // 验证deviceId 是否存在
                List<SceneDevice> targetData = sceneDeviceRepository.findByUnrealName(sourceData.getUnrealName());
                if (targetData != null && targetData.size() > 0) {
                    sourceData.setStationId(targetData.get(0).getStationId());
                    sourceData.setStationName(targetData.get(0).getStationName());
                    // 存储
                    sceneDeviceRelationRepository.save(sourceData);
                    t = 1;
                } else {
                    t = 0;
                }
            } else {
                SceneDeviceRelation pojo = sceneDeviceRelationRepository.getById(sourceData.getId());
                if (pojo != null) {
                    if (!Objects.isNull(sourceData.getUnrealName())) {
                        pojo.setUnrealName(sourceData.getUnrealName());
                    }
                    if (!StringUtils.isEmpty(sourceData.getType())) {
                        pojo.setType(sourceData.getType());
                    }
                    if (!StringUtils.isEmpty(sourceData.getEquipmentId())) {
                        pojo.setEquipmentId(sourceData.getEquipmentId());
                    }
                    sceneDeviceRelationRepository.save(pojo);
                    t = 1;
                } else {
                    t = 0;
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new Exception(e.getMessage());
        } finally {
            return t;
        }
    }

    /**
     * 存储信息
     *
     * @param sourceDataList
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveAll(List<SceneDeviceRelation> sourceDataList) throws Exception {
        int t = 0;
        try {
            if (Objects.isNull(sourceDataList)) {
                sceneDeviceRelationRepository.saveAll(sourceDataList);
                t = sourceDataList.size();
            } else {
                t = 0;
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new Exception(e.getMessage());
        } finally {
            return t;
        }
    }

    /**
     * 删除信息根据id
     *
     * @param idS
     * @throws Exception
     */
    @Override
    public void deleteById(String idS) throws Exception {
        try {
            if (!StringUtils.isEmpty(idS)) {
                String[] ids = idS.split(",");
                for (String item : ids) {
                    int id = Integer.parseInt(item);
                    sceneDeviceRelationRepository.deleteById(id);
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 分页查询数据
     *
     * @param example  查询条件
     * @param pageable 分页项
     * @return
     * @throws Exception
     */
    @Override
    public Page<SceneDeviceRelation> findPageData(Specification<SceneDeviceRelation> example, Pageable pageable) throws Exception {
        try {
            Page<SceneDeviceRelation> pageData = sceneDeviceRelationRepository.findAll(example, pageable);
            return pageData;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 查询数据
     *
     * @param example  查询条件
     * @return
     * @throws Exception
     */
    @Override
    public List<SceneDeviceRelation> findData(Specification<SceneDeviceRelation> example) throws Exception {
        try {
            List<SceneDeviceRelation> pageData = sceneDeviceRelationRepository.findAll(example);
            return pageData;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 查询信息
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<SceneDeviceRelation> findAll() throws Exception {
        try {
            List<SceneDeviceRelation> targetData = sceneDeviceRelationRepository.findAll();
            return targetData;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new Exception(e.getMessage());
        }
    }


    /**
     * 查询信息根据站场id
     *
     * @param deviceIds
     * @return
     * @throws Exception
     */
    @Override
    public List<SceneDeviceRelation> findByUnrealName(String unrealNames) throws Exception {
        try {
            List<SceneDeviceRelation> targetData = sceneDeviceRelationRepository.findByUnrealName(Arrays.asList(unrealNames.split(",")));
            return targetData;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new Exception(e.getMessage());
        }
    }
}
